package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.AgencyInfo;
import cn.wolfcode.crm.domain.ProductInfo;
import cn.wolfcode.crm.query.AccidentChartQueryObject;
import cn.wolfcode.crm.query.SaleChartQueryObject;
import cn.wolfcode.crm.service.IAccidentChartService;
import cn.wolfcode.crm.service.IAgencyInfoService;
import cn.wolfcode.crm.service.IProductInfoService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("accidentChart")
public class AccidentChartController {
    @Autowired
    private IAccidentChartService service;
    @Autowired
    private IAgencyInfoService agencyInfo;
    @Autowired
    private IProductInfoService productInfoService;
    @RequestMapping("")
    private String index(){
        return "accidentChart/list";
    }
    @RequestMapping("list")
    @ResponseBody
    private List<Map<String, Object>> list(AccidentChartQueryObject qo){
        return service.accidentForChart(qo);
    }
    @RequestMapping("selectAgencyName")
    @ResponseBody
    private List<AgencyInfo> selectAgencyName(){
        return agencyInfo.selectAll();
    }
    @RequestMapping("selectProductName")
    @ResponseBody
    private List<ProductInfo> selectProductName(){
        return productInfoService.selectAll();
    }
    @RequestMapping("/queryByPic")
    public String saleByPie(Model model, AccidentChartQueryObject qo) {
        model.addAttribute("groupByType", AccidentChartQueryObject.groupByTypes.get(qo.getGroupByType()));
        //获取到所有的分组类型
        List<Map<String, Object>> saleChart = service.accidentForChart(qo);
        List<Object> groupByTypes = new ArrayList<>();
        List<Map<String, Object>> datas = new ArrayList<>();
        for (Map<String, Object> map : saleChart) {
            groupByTypes.add(map.get("groupByType"));
            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", map.get("groupByType"));
            map2.put("value", map.get("totalCompensation"));
            datas.add(map2);
        }
        model.addAttribute("groupByTypes", JSON.toJSONString(groupByTypes));
        model.addAttribute("datas", JSON.toJSONString(datas));
        return "accidentChart/saleChartByPic";
    }
    @RequestMapping("/queryByBar")
    public String saleByBar(Model model, @ModelAttribute("qo")AccidentChartQueryObject qo) {

        model.addAttribute("groupByType", SaleChartQueryObject.groupByTypes.get(qo.getGroupByType()));
        List<Map<String, Object>> selectSaleChar = service.accidentForChart(qo);
        List<Object> groupByTypes = new ArrayList<>();
        List<Object> totalAmounts = new ArrayList<>();
        for (Map<String,Object> map : selectSaleChar) {
            groupByTypes.add(map.get("groupByType"));
            totalAmounts.add(map.get("totalCompensation"));
        }
        model.addAttribute("groupByTypes", JSON.toJSONString(groupByTypes));
        model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));
        return "accidentChart/saleChartByBar";
    }

}
